<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>硅基 KEY 池 - 登录</title>
    <link rel="icon" type="image/x-icon" href="/static/logo.png">
    <!-- 预加载字体 -->
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <style>
        :root {
            /* Color Palette */
            --primary-color: #4f46e5;
            --primary-hover: #4338ca;
            --primary-light: rgba(79, 70, 229, 0.1);
            --success-color: #10b981;
            --danger-color: #ef4444;
            --background-color: #f9fafb;
            --surface-color: #ffffff;
            --border-color: #e5e7eb;
            --text-primary: #111827;
            --text-secondary: #6b7280;
            --text-tertiary: #9ca3af;
            
            /* Typography */
            --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            
            /* Spacing */
            --space-xs: 0.25rem;
            --space-sm: 0.5rem;
            --space-md: 1rem;
            --space-lg: 1.5rem;
            --space-xl: 2rem;
            --space-2xl: 2.5rem;
            
            /* Borders */
            --radius-sm: 0.375rem;
            --radius-md: 0.5rem;
            --radius-lg: 0.75rem;
            --radius-xl: 1rem;
            
            /* Shadows */
            --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
            --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
            --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }

        /* Dark mode variables */
        @media (prefers-color-scheme: dark) {
            :root {
                --primary-color: #6366f1;
                --primary-hover: #4f46e5;
                --primary-light: rgba(99, 102, 241, 0.1);
                --success-color: #10b981;
                --danger-color: #ef4444;
                --background-color: #111827;
                --surface-color: #1f2937;
                --border-color: #374151;
                --text-primary: #f9fafb;
                --text-secondary: #e5e7eb;
                --text-tertiary: #d1d5db;
            }
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: var(--font-sans);
            background-color: var(--background-color);
            color: var(--text-primary);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: var(--space-md);
            line-height: 1.5;
            background-image: linear-gradient(135deg, #f5f7ff 0%, #f1f4ff 100%);
        }

        /* 适配深色模式的背景 */
        @media (prefers-color-scheme: dark) {
            body {
                background-image: linear-gradient(135deg, #111827 0%, #1f2937 100%);
            }
        }

        .login-container {
            background-color: var(--surface-color);
            border-radius: var(--radius-xl);
            box-shadow: var(--shadow-lg);
            width: 100%;
            max-width: 420px;
            overflow: hidden;
            position: relative;
        }

        .login-header {
            padding: var(--space-2xl) var(--space-2xl) var(--space-lg);
            text-align: center;
            position: relative;
            background-image: linear-gradient(135deg, rgba(79, 70, 229, 0.08) 0%, rgba(79, 70, 229, 0.03) 100%);
            border-bottom: 1px solid var(--border-color);
        }

        .logo-container {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-bottom: var(--space-lg);
        }

        .logo {
            width: 60px;
            height: 60px;
            object-fit: contain;
            border-radius: var(--radius-md);
        }

        h1 {
            color: var(--primary-color);
            font-size: 1.75rem;
            font-weight: 700;
            margin-top: var(--space-md);
            letter-spacing: -0.025em;
        }

        .login-subtitle {
            color: var(--text-secondary);
            font-size: 0.9rem;
            margin-top: var(--space-xs);
        }

        .login-form {
            padding: var(--space-xl) var(--space-2xl) var(--space-2xl);
        }

        .form-group {
            margin-bottom: var(--space-lg);
        }

        label {
            display: block;
            margin-bottom: var(--space-sm);
            font-weight: 500;
            color: var(--text-primary);
            font-size: 0.9rem;
        }

        .input-wrapper {
            position: relative;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 1px solid var(--border-color);
            border-radius: var(--radius-md);
            font-size: 1rem;
            transition: all 0.2s ease;
            background-color: var(--surface-color);
            color: var(--text-primary);
        }

        input[type="text"]:focus,
        input[type="password"]:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px var(--primary-light);
        }

        .btn {
            width: 100%;
            padding: 0.85rem;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: var(--radius-md);
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 0.5rem;
        }

        .btn:hover {
            background-color: var(--primary-hover);
            transform: translateY(-1px);
        }

        .btn:active {
            transform: translateY(0);
        }

        .btn:disabled {
            opacity: 0.7;
            cursor: not-allowed;
        }

        .error-message {
            color: var(--danger-color);
            font-size: 0.9rem;
            margin-top: var(--space-lg);
            text-align: center;
            display: none;
            padding: 0.75rem;
            background-color: rgba(239, 68, 68, 0.1);
            border-radius: var(--radius-md);
            border: 1px solid rgba(239, 68, 68, 0.2);
        }

        .info-text {
            text-align: center;
            margin-top: var(--space-lg);
            color: var(--text-tertiary);
            font-size: 0.875rem;
        }

        @media (max-width: 480px) {
            .login-header, 
            .login-form {
                padding-left: var(--space-lg);
                padding-right: var(--space-lg);
            }
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* Input validation styles */
        .input-wrapper.error input {
            border-color: var(--danger-color);
            box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
        }

        .input-error {
            color: var(--danger-color);
            font-size: 0.8rem;
            margin-top: 0.25rem;
            display: none;
        }

        .input-wrapper.error .input-error {
            display: block;
        }
    </style>
</head>

<body>
    <div class="login-container">
        <div class="login-header">
            <div class="logo-container">
                <img src="/static/logo.png" alt="Logo" class="logo">
            </div>
            <h1>硅基 KEY 池</h1>
            <p class="login-subtitle">API 密钥管理平台</p>
        </div>
        
        <form id="loginForm" class="login-form">
            <div class="form-group">
                <label for="username">用户名</label>
                <div class="input-wrapper" id="usernameWrapper">
                    <input type="text" id="username" name="username" required autocomplete="username" placeholder="输入您的用户名">
                    <div class="input-error">请输入用户名</div>
                </div>
            </div>
            
            <div class="form-group">
                <label for="password">密码</label>
                <div class="input-wrapper" id="passwordWrapper">
                    <input type="password" id="password" name="password" required autocomplete="current-password" placeholder="输入您的密码">
                    <div class="input-error">请输入密码</div>
                </div>
            </div>
            
            <button type="submit" class="btn" id="loginButton">登录</button>
            <div id="errorMessage" class="error-message"></div>
        </form>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const usernameInput = document.getElementById('username');
            const passwordInput = document.getElementById('password');
            const usernameWrapper = document.getElementById('usernameWrapper');
            const passwordWrapper = document.getElementById('passwordWrapper');
            
            // 输入框验证
            usernameInput.addEventListener('input', function() {
                if (this.value.trim()) {
                    usernameWrapper.classList.remove('error');
                }
            });
            
            passwordInput.addEventListener('input', function() {
                if (this.value.trim()) {
                    passwordWrapper.classList.remove('error');
                }
            });
            
            // 在新的登录尝试时清除错误消息
            usernameInput.addEventListener('focus', clearErrorMessage);
            passwordInput.addEventListener('focus', clearErrorMessage);
            
            function clearErrorMessage() {
                document.getElementById('errorMessage').style.display = 'none';
            }
        });
        
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            // 验证表单
            let isValid = true;
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();
            const usernameWrapper = document.getElementById('usernameWrapper');
            const passwordWrapper = document.getElementById('passwordWrapper');
            const errorMessage = document.getElementById('errorMessage');
            const loginButton = document.getElementById('loginButton');
            
            if (!username) {
                usernameWrapper.classList.add('error');
                isValid = false;
            } else {
                usernameWrapper.classList.remove('error');
            }
            
            if (!password) {
                passwordWrapper.classList.add('error');
                isValid = false;
            } else {
                passwordWrapper.classList.remove('error');
            }
            
            if (!isValid) {
                return;
            }
            
            // 清除之前的错误消息
            errorMessage.style.display = 'none';
            
            // 禁用按钮并显示加载状态
            loginButton.disabled = true;
            loginButton.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="animation: spin 1s linear infinite;"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg> 登录中...';
            
            try {
                const response = await fetch('/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ username, password })
                });
                
                if (response.ok) {
                    // 登录成功，重定向到管理页面
                    window.location.href = '/admin';
                } else {
                    const data = await response.json();
                    errorMessage.textContent = data.detail || '登录失败，请检查用户名和密码';
                    errorMessage.style.display = 'block';
                    
                    // 保持错误消息可见，不自动消失
                    
                    // 恢复按钮状态
                    loginButton.disabled = false;
                    loginButton.textContent = '登录';
                }
            } catch (error) {
                errorMessage.textContent = '登录请求失败，请稍后再试';
                errorMessage.style.display = 'block';
                
                // 恢复按钮状态
                loginButton.disabled = false;
                loginButton.textContent = '登录';
            }
        });
    </script>
</body>

</html>